##=============================================================================
## Main Manuscript: Table 3
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "lme4", "sjPlot", "ordinal", "texreg", "hrbrthemes")


ipak(packages)

##---------
# Load Data
#$setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##-----
# Fit 1
#str(dat$save_lives)
dat$save_lives <- factor(dat$save_lives)

fit1 <- clmm(save_lives ~ 1 + wave + survival_mindset + per_idx_invcov + 
               fam_idx_invcov + frontline_b + ln_viina_two_months +
               displaced + sex + rus_ethnic + age + edu + job  + finances +
               (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                  adm1_en), data = dat)

summary(fit1)
#sjPlot::tab_model(fit1)
#performance::icc(fit1)
#sjPlot::plot_model(fit1)
#AIC(logLik(fit1))

##-----
# Fit 2
fit2 <- glmer(compromise ~ 1 + wave + survival_mindset + per_idx_invcov + 
                fam_idx_invcov + frontline_b + ln_viina_two_months +
                displaced + sex + rus_ethnic + age + edu + job + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                   adm1_en), 
              data = dat, 
              family = binomial,
              control = glmerControl(optimizer = "bobyqa"))

summary(fit2)
#sjPlot::tab_model(fit2)
#sjPlot::plot_model(fit2)
#performance::icc(fit2)
#AIC(logLik(fit2))

##-----
# Fit 3
fit3 <- lmer(compromise_idx_terr_z ~ 1 + wave + survival_mindset + 
                per_idx_invcov + fam_idx_invcov + frontline_b + 
                ln_viina_two_months + displaced + rus_ethnic + sex + age + 
                edu + job + ln_viina_two_months + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + 
                              fam_idx_invcov | adm1_en), data = dat)

summary(fit3)
#sjPlot::tab_model(fit3)
#sjPlot::plot_model(fit3)
#performance::icc(fit3)
#AIC(logLik(fit3))

##-----
# Fit 4
fit4 <- glmer(ukr_no_nato ~ 1 + wave + survival_mindset + per_idx_invcov + 
                fam_idx_invcov + frontline_b + ln_viina_two_months +
                displaced + sex + rus_ethnic + age + edu + job + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                   adm1_en), 
              data = dat, 
              family = binomial,
              control = glmerControl(optimizer = "bobyqa"))

summary(fit4)
#sjPlot::tab_model(fit4)
#sjPlot::plot_model(fit4)
#performance::icc(fit4)
#AIC(logLik(fit4))

##-----
# Fit 5
fit5 <- glmer(rus_lang_schools ~ 1 + wave + survival_mindset + per_idx_invcov + 
                fam_idx_invcov + frontline_b + ln_viina_two_months +
                displaced + sex + rus_ethnic + age + edu + job + finances + 
                (1 + wave + survival_mindset + per_idx_invcov + fam_idx_invcov | 
                   adm1_en), 
              data = dat, 
              family = binomial,
              control = glmerControl(optimizer = "bobyqa"))

summary(fit5)
#sjPlot::tab_model(fit5)
#sjPlot::plot_model(fit5)
#AIC(logLik(fit5))

##----------------------
# Export models to LaTeX
labs <- c("(Intercept)",
          "Survey Wave",
          "Survival Mindset",
          "Personal Victimization",
          "Family Victimization",
          "Frontline Oblast",
          "Ln(VIINA)",
          "Displaced",
          "Sex",
          "Ethnic Russian",
          "Age",
          "Education",
          "Employment",
          "Socio-Economic Status",
          "1|2",
          "2|3",
          "3|4")

texreg(list(fit1, fit2, fit3, fit4, fit5), 
       dcolumn = TRUE,
       #custom.coef.names = labs,
       custom.model.names = c("Save Lives (ordinal)", 
                              "Compromise (binary)",
                              "Compromise (terr index)",
                              "Compromise (ukr no nato",
                              "Compromise (rus lang schools)"))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================